<?php
	function get_latest_events($limit = 3)
	{
		global $connection;
		
		
		$query = "SELECT ed.*, e.id AS id, e.start_date, e.end_date, e.city_id FROM events_details ed
					  LEFT JOIN events e ON ed.event_id = e.id 
				    WHERE ed.language_id = ? ";
		$parameters[] = $_SESSION["language"]["id"];
		
		$query .= " GROUP BY e.id ORDER BY e.start_date DESC";
		
		if($limit > 0)
		{
			 $query .= " LIMIT $limit";
		}
		
		$events = $connection->fetch_result($query, $parameters);
		foreach($events as $key => $event)
		{
			$events[$key]["images"] = get_event_images($event["id"]);
		}
		return $events;
	}
	
	
	function get_event($eventURL, $languageID)
	{
		global $connection;
		$event = $connection->fetch_one("SELECT ed.*, e.start_date, e.end_date, e.id AS id, e.city_id FROM events_details ed 
												LEFT JOIN events e ON ed.event_id = e.id 
												WHERE ed.url = ? AND ed.language_id = ?", array($eventURL, $languageID));
		$event["images"] = get_event_images($event["id"]);
		
		return $event;
	}
	
	function get_event_products($eventID)
	{
		global $connection;
		
		$products = $connection->fetch_result("SELECT * FROM events_products WHERE event_id = ?", array($eventID));
		$product_ids = false;
		
		foreach ($products as $key => $product) 
		{
			$product_ids[$product["product_id"]] = $product["product_id"];
		}
		if($product_ids)
		{
			$products = getProductByField($product_ids, "id");
		}
		
		return $products;
	}
	
	function get_event_images($articleID)
	{
		global $connection;
		
		$photos = $connection->fetch_result("SELECT pf.title, pf.name, pf.thumb_name FROM events_pictures ep
												LEFT JOIN picture_files pf ON ep.picture_id = pf.id
												WHERE ep.event_id = ?", array($articleID));
		return $photos;
	}
	
	
?>